﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using MessageProtocol;

namespace ClientTest
{
    class Client
    {
        private Socket clientSocket;
        byte[] byteMsg;
        Message reqMsg;
        Message respMsg;

        static void Main(string[] args)
        {            
            Console.Write("This is Client");

            BodyRequest reqBody = new BodyRequest();
            reqBody.PLAYER_NAME = Encoding.UTF8.GetBytes("Player1");
            reqBody.SCORE = 231252;

            Header header = new Header();
            header.MSGID = 2;
            header.MSGTYPE = MessageConstants.TYPE_REQUEST_SAVE;
            header.BODYLENGTH = (uint)reqBody.GetSize();
            header.LASTMSG = 1;
            
            Message test = new Message()
            {
            Body = reqBody, Header = header  
            };

            BodyRequest testBody = (BodyRequest)test.Body;
            Console.WriteLine("\nd : {0}" + testBody.GetSize());
            ConnectToServer("127.0.0.1", 62626, test);
            
            while (true) ;
        }

        public void Connect(string ipString, int portNum)
        {
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ipServer = IPAddress.Parse(ipString);
            clientSocket.Connect(new IPEndPoint(ipServer, portNum));
        }

        public void Send(Message msg)
        {
            byteMsg = msg.GetBytes();
            clientSocket.BeginSend(byteMsg, 0, byteMsg.Length, SocketFlags.None, new AsyncCallback(OnSend), clientSocket);
        }

        private void OnSend(IAsyncResult ar)
        {
            clientSocket = (Socket)ar.AsyncState;
            int Size = clientSocket.EndSend(ar);

            Stream stream = new MemoryStream(byteMsg);
            reqMsg = MessageController.Decode(new MemoryStream(byteMsg));
            clientSocket.BeginReceive(byteMsg, 0, byteMsg.Length, SocketFlags.None, new
                AsyncCallback(OnReceive), clientSocket);
        }

        public void OnReceive(IAsyncResult ar)
        {
            clientSocket = (Socket)ar.AsyncState;
            int Size = clientSocket.EndReceive(ar);
            respMsg = MessageController.Decode(new MemoryStream(byteMsg));
        }


        public static void ConnectToServer(string ipString, int portNum, Message reqMsg)
        {
            try
            {
                IPEndPoint clientAddress = new IPEndPoint(0, 0);
                IPEndPoint serverAddress = new IPEndPoint(IPAddress.Parse(ipString), portNum);

                Console.Write("서버 : {0}\n", serverAddress.ToString());

                TcpClient client = new TcpClient(clientAddress);
                client.Connect(serverAddress);

                NetworkStream stream = client.GetStream();
                MessageController.Encode(stream, reqMsg);

                Message rspMsg = MessageController.Decode(stream);

            }
            catch (SocketException e)
            {
                Console.Write(e);
            }

        }
    
    }
}
